/**
 * 
 */
package com.example.myandroidapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FlickrPhotoViewerActivity extends Activity
{
    private static final String TAG = "FlickrPhotoViewerActivity";

    private Bundle mBundle;

    private List<Photo> myList = new ArrayList<Photo>();

    private ProgressDialog myProgress;

    public class DataLoadingTask extends AsyncTask<Void, Void, Void>
    {

        
        @Override
        protected void onPostExecute(Void result)
        {

            Log.d(TAG, "----------------about to call display");
            // display();

        }

        @Override
        protected void onPreExecute()
        {

            myProgress = new ProgressDialog(FlickrPhotoViewerActivity.this);

            myProgress.setMessage("Please wait..");

            myProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Void doInBackground(Void... params)
        {
            // TODO Auto-generated method stub
            try
            {
                read();
            }
            catch (IOException ex)
            {
                Log.e(TAG, "-----exception was " + ex.toString());
            }
            
            return null;
        }
    }
    
    public class MyCustomAdapterAll extends ArrayAdapter<Person>
    {
        public MyCustomAdapterAll(Context context, int textViewResourceId, List<Person> theListAll)
        {
            super(context, textViewResourceId, theListAll);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            // TODO Auto-generated method stub
            // return super.getView(position, convertView, parent);

            View row = convertView;


            if (row == null)
            {
                LayoutInflater inflater = getLayoutInflater();
                row = inflater.inflate(R.layout.row_contacts, parent, false);
            }

            TextView list_name = (TextView) row.findViewById(R.id.list_name);

            TextView list_tagline = (TextView) row.findViewById(R.id.list_tagline);

            list_name.setText(myList.get(position).getId());

            list_tagline.setText("Age : " + myList.get(position).getTitle());

            ImageView contactlogo = (ImageView) row.findViewById(R.id.contactlogo);

            Drawable myAvatar = FlickrPhotoViewerActivity.this.getResources().getDrawable(R.drawable.avatar_male_72x72);

            int resID = getBaseContext().getResources().getIdentifier("avatar_male_72x72", "drawable", "com.example.com.dastest.myapp");

            contactlogo.setImageResource(resID);

            return row;
        }
    }

    @Override
    public void onCreate(Bundle mybundle)
    {

        super.onCreate(mybundle);
        mBundle = this.getIntent().getExtras();

        Log.d(TAG, "The value of mBundle string is " + mBundle.getString("mykey"));
        
        new DataLoadingTask().execute();
        
        
    }

    public void onResume()
    {
        super.onResume();
        Log.d(TAG, "This is being called when the activity comes to the foreground" + mBundle.getString("mykey"));
    }

    public void onPause()

    {
        super.onPause();
        Log.d(TAG, "onPause is about to be triggered");
        mBundle.putString("mykey", "myvalue2");
    }

    public void display()
    {
        // TODO do display stuff
    }
    
    public void read() throws IOException
    {
        ObjectsReader or = new ObjectsReader();
        
        FlickrPhotoResponse myResponse = or.readPhotoResponse(FeaturesConfiguration.getFlickrurl());
        Log.d(TAG, myResponse.toString());
        
//        for (Photo photo : myList)
//        {
//            Log.d(TAG, "photo title : " + photo.getTitle());
//            Log.d(TAG, "photo id : " + photo.getId());
//        }
        
    }

}
